package com.mingqi.enetconnected.view.Dialog.tree.indicator;

import android.database.Observable;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;

/**
 * @author yaxi
 * @date 2018/9/18
 */
public abstract class AbstractAdapter<T> {

    public abstract int getCount();

    public abstract View getView(int position, ViewGroup parent);

    public abstract View addView(int position, ViewGroup parent);

    public abstract void removeView(int position);

    public abstract T getData(int position);

    public abstract void addData(T t);

    public abstract void addData(int position,T t);

    public abstract void setData(int position,T t);

    protected final AdapterDataObservable mObservable = new AdapterDataObservable();

    public void registerAdapterDataObserver(@NonNull AbstractAdapterDataObserver observer) {
        mObservable.registerObserver(observer);
    }

    public void unregisterAdapterDataObserver(@NonNull AbstractAdapterDataObserver observer) {
        mObservable.unregisterObserver(observer);
    }

    public abstract static class AbstractAdapterDataObserver{

        public void onChanged(){}

        public void onItemAdd(){}

        public void onItemAdd(int position){}

        public void OnItemChanged(int position){}

        public void onItemRemoved(int position){}

        public void onRangeItemRemoved(int start,int count){}

    }




    static class AdapterDataObservable extends Observable<AbstractAdapterDataObserver> {
        public boolean hasObservers() {
            return !mObservers.isEmpty();
        }

        public void notifyChanged() {
            for (int i = mObservers.size() - 1; i >= 0; i--) {
                mObservers.get(i).onChanged();
            }
        }

        public void notifyItemAdd(){
            for (int i = mObservers.size() - 1; i >= 0; i--) {
                mObservers.get(i).onItemAdd();
            }
        }

        public void notifyItemAdd(int position) {
            for (int i = mObservers.size() - 1; i >= 0; i--) {
                mObservers.get(i).onItemAdd(position);
            }
        }

        public void notifyItemChanged(int position){
            for (int i = mObservers.size() - 1; i >= 0; i--) {
                mObservers.get(i).OnItemChanged(position);
            }
        }

        public void notifyItemRemoved(int position){
            for (int i = mObservers.size() - 1; i >= 0; i--) {
                mObservers.get(i).onItemRemoved(position);
            }
        }

        public void notifyItemRangeChanged(int start,int count){
            for (int i = mObservers.size() - 1; i >= 0; i--) {
                mObservers.get(i).onRangeItemRemoved(start, count);
            }
        }

    }
}
